<script lang="ts" setup>
const props = defineProps({
  modelValue: {
    type: Object,
    required: true,
  },
  poolType: {
    type: Number,
  },
  prefix: {
    type: String,
    default: "",
  },
});

const emits = defineEmits(["update:modelValue"]),
  currencyList: Array<any> = inject("currencyList", []);

const form = ref({
  priceType: 1,
  payPrice: 0,
});

const detail = computed({
  get: () => {
    let detail = Object.assign({}, form.value, props.modelValue);
    let keys = Object.keys(form.value);
    for (let i = 0; i < keys.length; i++) {
      if (props.modelValue[keys[i]] === undefined) {
        emits("update:modelValue", detail);
        break;
      }
    }
    return detail;
  },
  set: (val) => {
    emits("update:modelValue", val);
  },
});

const onChange = () => {
  detail.value = detail.value;
};
</script>
<template>
  <el-form-item label="计价类型:" :prop="`${prefix}priceType`">
    <el-radio-group v-model="detail.priceType" @change="onChange">
      <el-radio-button :label="1">现金</el-radio-button>
      <!-- 非抽奖的商品可配置积分购买 -->
      <!-- 礼品包开放积分类型 -->
      <!-- <template v-if="poolType == 2"> -->
        <el-radio-button :label="2">积分</el-radio-button>
        <el-radio-button :label="3">现金 + 积分</el-radio-button>
      <!-- </template> -->
    </el-radio-group>
  </el-form-item>
  <el-form-item v-if="detail.priceType == 1 || detail.priceType == 3" label="售价:" :prop="`${prefix}payPrice`"
    :rules="[{ required: true, message: '请填写价格', trigger: 'blur' }]">
    <EasyNumberInput v-model="detail.payPrice" @change="onChange" />
  </el-form-item>
  <template v-if="detail.priceType == 2 || detail.priceType == 3">
    <el-form-item label="积分类型:" :prop="`${prefix}currencyId`" :rules="[
      {
        required: true,
        message: '请选择积分类型',
        trigger: 'change',
      },
    ]">
      <el-select v-model="detail.currencyId" placeholder="请选择" style="width: 100px" @change="onChange">
        <el-option v-for="(item, key) in currencyList" :label="item.name" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="积分数量:" :prop="`${prefix}currencyNum`" :rules="[
      {
        required: true,
        message: '请填写积分数量',
        trigger: 'blur',
      },
    ]">
      <EasyNumberInput v-model="detail.currencyNum" :min="0" unit="个" :precision="0" @change="onChange" />
    </el-form-item>
  </template>
</template>
